(function(){
	$(function(){
		/*
		$('.js-join-cart').click(function(event) {
			event.preventDefault();
			if(_userId<0){
				alert('请先登录');
				return false;
			}
			api.solo('cartAdd',[
				$(this).closest('.goods').attr('data-gopenid'),
				0,
				1,//数量
			],function(d){
				if(d.state==200){
					alert('添加成功')
					//加一个
					$('.control-cart-num').text(parseInt($('.control-cart-num').text(),10)+1)
				}else{
					alert(d.title)
				}
			})
		});
		*/
		var pageType = $('#container').attr('data-type');
		if(pageType=='collect'){
			var apiType = 'removeFromCollect'
		}else if(pageType == 'browser'){
			var apiType = 'deleteBrowse'
		}else{
			return false;
		}

		$('.js-remove-collect').click(function(e){
			var that = $(this)
			e.preventDefault();
			if(_userId<0){
				alert('请先登录');
				return false;
			}
			api.solo(apiType,[$(this).closest('.goods').attr('data-id')],function(d){
				if(d.resultCode==200){
					//alert('成功')
					//加一个
					that.closest('.goods').remove()
					console.log(that.closest('.goods'))
				}else{
					alert('删除失败')
				}
			})
		})

		//用户分享
		var shareButton  = $('.goods-share a');
		var alertBox     = $('.alert-bg').hide();
		var closeBuutton = alertBox.find('.close')
		var textUrl      = alertBox.find('.url-show')
		var copyButton   = alertBox.find('.copy-url-button')
		var goodsUrl     = _trueRoot + "goods/index";
		var shareUrlBox  = $('.alert-box-share-url')
		var shareUserBox = $('.alert-box-share-user')
		var showUserNum  = 20;
		var shareUserTpl = {
			loading  : shareUserBox.find('h5').show(),
			userlist : shareUserBox.find('.share-user-list').hide(),
			name     : shareUserBox.find('.user-name').remove(),
			split    : shareUserBox.find('.user-name-split').remove(),
			numBox   : shareUserBox.find('.share-user-num-box').hide(),
			num      : shareUserBox.find('.share-user-num').show(),
			noUser   : shareUserBox.find('.share-no-user').hide()
		};
		var shareUserListBox = $('.alert-box-share-user .share-user-list');

		shareButton.click(function(e){
			e.preventDefault()

			var gopen_id = $(this).closest('.goods').attr('data-gopenid');
			var type = $(this).attr('data-type');

			shareUrlBox.hide()
			shareUserBox.hide()

			if(type=='user'){
				showShareUser(gopen_id)
			}else if(type=='url'){
				showShareUrl(gopen_id)
			}else{
				return false;
			}


			alertBox.show()
		})
		closeBuutton.click(function(e){
			e.preventDefault()
			alertBox.hide()
		})

		function showShareUrl(goodsid){
			var url = getShareInfo(goodsid);
			textUrl.html(url);
			copyButton.attr('data-clipboard-text', url);
			copyToBoard(copyButton)
			jiathis_config.url = url;

			shareUrlBox.show()
		}
		function showShareUser(gopen_id){
			shareUserListBox.empty();
			shareUserTpl.loading.show();//显示加载中
			shareUserTpl.userlist.hide();
			shareUserTpl.numBox.hide();
			shareUserTpl.noUser.hide();
			shareUserBox.show();//打开
			api.solo('getGoodsShareUsers',[_userId,gopen_id,showUserNum],function(d){
				//if(d.resultCode != 200){
				//	alert('获取分享用户失败')
				//	return false;
				//}
				shareUserTpl.loading.hide();
				var list = d.data.list;
				if(list.length == 0){
					shareUserTpl.noUser.show();
					return false;
				}

				for(var i in list){
					console.log(i)
					var nameItem = shareUserTpl.name.clone().html(list[i].username)
					var splitItem = shareUserTpl.split.clone()
					shareUserTpl.userlist.append(nameItem).append(splitItem);
				}
				shareUserTpl.num.html(d.data.total);


				shareUserTpl.userlist.show()
				if(d.data.total > list.length)
					shareUserTpl.numBox.show()

			})
		}

		function getShareInfo(goodsid){
			var url = goodsUrl;
			var queryGoods = '?id='+ goodsid;
			var queryUid = '&fromUid=' + _userId;
			var finalUrl = url + queryGoods + queryUid;
			return finalUrl;
		}

		function copyToBoard($button){
			var clip = new ZeroClipboard( $button.get(0) );
			 
			clip.on( "load", function(client) {
			 
			  client.on( "complete", function(client, args) {
			    // `this` is the element that was clicked
			    // this.style.display = "none";
			    alert("已经复制到剪切板 ，如果失败请手动复制下面的地址");
			  } );
			} );
		}
	})
})()




